home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
pascal
/
totsrc11.zip
/
TOTDATE.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-05-04
|
16KB
|
615 lines
{ Copyright 1991 TechnoJock Software, Inc. }
{ All Rights Reserved }
{ Restricted by License }
{ Build # 1.10 }
Unit totDATE;
{$I TOTFLAGS.INC}
{
Development Notes:
1.00a ??/??/?? Corrected calc of Year
1.00b 06/01/91 Corrected 1.00a!
1.00c 02/03/92 Changed Leap Year validation
1.00d 02/27/92 Corrected DateFormat function
1.00e 03/09/92 Changed 1900+ operation for 0..99 years
1.10a 05/05/93 Changed ret value on JultoGreg and GregToStr
when 0 values passed.
}
INTERFACE
Uses DOS,totLOOK,totSTR;
Type
tDate = (MMDDYY,MMDDYYYY,MMYY,MMYYYY,DDMMYY,DDMMYYYY,YYMMDD,YYYYMMDD);
StrShort = string[20];
tMonths = array[1..12] of StrShort;
tDays = array[0..6] of StrShort;
pDateOBJ = ^DateOBJ;
DateOBJ = object
vLastYearNextCentury: byte;
vSeparator: char;
vMonths: tMonths;
vDays: tDays;
{methods...}
constructor Init;
procedure SetLastYearNextCentury(Yr:byte);
procedure SetSeparator(Sep:char);
procedure SetMonths(Mth1,Mth2,Mth3,Mth4,Mth5,Mth6,Mth7,Mth8,Mth9,Mth10,Mth11,Mth12: strshort);
procedure SetDays(Day0,Day1,Day2,Day3,Day4,Day5,Day6:strshort);
function GetLastYearNextCentury: byte;
function GetSeparator: char;
function GetMonth(Mth:byte):string;
function GetDay(Day:byte):string;
destructor Done;
end; {DateOBJ}
function GregtoJul(M,D,Y : longint): longint;
procedure JultoGreg(Jul:longint; var M,D,Y: longint);
function Day(DStr:string;Format:tDate): word;
function Month(DStr:string;Format:tDate): word;
function Year(DStr:string;Format:tDate): word;
function StrtoJul(DStr:string;Format:tDate):longint;
function DOWStr(DStr:string;Format:tDate): byte;
function DOWJul(Jul:longint): byte;
function GregtoStr(M,D,Y:longint;Format:tDate): string;
function JultoStr(Jul:longint;Format:tDate): string;
function TodayinJul: longint;
function ValidDate(M,D,Y:longint):boolean;
function ValidDateStr(DStr:string;Format:tDate): boolean;
function StripDateStr(DStr:string;Format:tDate):string;
function FancyDateStr(Jul:longint; Long,Day:boolean): string;
function RelativeDate(DStr:string;Format:tDate;Delta:longint):string;
function StartOfYear(Jul:longint):longint;
function EndOfYear(Jul:longint):longint;
function DateFormat(Format:tDate):string;
procedure DateInit;
var
DateTOT: ^DateOBJ;
IMPLEMENTATION
{|||||||||||||||||||||||||||||||||||||||}
{ }
{ D a t e O B J M E T H O D S }
{ }
{|||||||||||||||||||||||||||||||||||||||}
constructor DateOBJ.Init;
{}
begin
vLastYearNextCentury := 20;
vSeparator := '/';
SetDays('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
SetMonths('January','February','March','April','May',
'June','July','August','September',
'October','November','December');
end; {DateOBJ.Init}
function DateOBJ.GetLastYearNextCentury: byte;
{}
begin
GetLastYearNextCentury := vLastYearNextCentury;
end; {DateOBJ.GetLastYearNextCentury}
procedure DateOBJ.SetLastYearNextCentury(Yr:byte);
{}
begin
{$IFDEF CHECK}
if (Yr >= 0) and (Yr <= 99) then
vLastYearNextCentury := Yr;
{$ELSE}
vLastYearNextCentury := Yr;
{$ENDIF}
end; {DateOBJ.GetLastYearNextCentury}
function DateOBJ.GetSeparator: char;
{}
begin
GetSeparator := vSeparator;
end; {DateOBJ.GetSeparator}
procedure DateOBJ.SetSeparator(Sep:char);
{}
begin
vSeparator := Sep;
end; {DateOBJ.SetSeparator}
procedure DateOBJ.SetMonths(Mth1,Mth2,Mth3,Mth4,Mth5,Mth6,Mth7,Mth8,Mth9,Mth10,Mth11,Mth12: StrShort);
{}
begin
vMonths[1] := Mth1;
vMonths[2] := Mth2;
vMonths[3] := Mth3;
vMonths[4] := Mth4;
vMonths[5] := Mth5;
vMonths[6] := Mth6;
vMonths[7] := Mth7;
vMonths[8] := Mth8;
vMonths[9] := Mth9;
vMonths[10] := Mth10;
vMonths[11] := Mth11;
vMonths[12] := Mth12;
end; {DateOBJ.SetMonths}
procedure DateOBJ.SetDays(Day0,Day1,Day2,Day3,Day4,Day5,Day6:StrShort);
{}
begin
vDays[0] := Day0;
vDays[1] := Day1;
vDays[2] := Day2;
vDays[3] := Day3;
vDays[4] := Day4;
vDays[5] := Day5;
vDays[6] := Day6;
end; {DateOBJ.SetDays}
function DateOBJ.GetMonth(Mth:byte):string;
{}
begin
if Mth in [2..12] then
GetMonth := vMonths[Mth]
else
GetMonth := vMonths[1];
end; {DateOBJ.GetMonth}
function DateOBJ.GetDay(Day:byte):string;
{}
begin
if Day in [1..6] then
GetDay := vDays[Day]
else
GetDay := vDays[0];
end; {DateOBJ.GetDay}
destructor DateOBJ.Done;
begin end;
{|||||||||||||||||||||||||||||||||||||||||||}
{ }
{ M i s c P r o c & F u n c s }
{ }
{|||||||||||||||||||||||||||||||||||||||||||}
function PadDateStr(DStr:string;Format:tDate):string;
{}
var
Part1,Part2,Part3: string;
L,P: byte;
Sep1,Sep2:char;
procedure PadOut(var S:string; width:byte);
begin
S := padright(S,width,'0');
end;
begin
if length(DStr) = length(DateFormat(Format)) then
begin
PadDateStr := DStr;
exit;
end;
P := 0;
L := length(DStr);
repeat
inc(P);
until (not (DStr[P] in ['0'..'9'])) or (P > L);
if P > L then
begin
PadDateStr := DStr;
exit;
end;
Part1 := copy(DStr,1,pred(P));
Sep1 := DStr[P];
delete(DStr,1,P);
P:= 0;
repeat
inc(P);
until (not (DStr[P] in ['0'..'9'])) or (P > L);
Part2 := copy(DStr,1,pred(P));
Sep2 := DStr[P];
Part3 := copy(DStr,succ(P),4);
case Format of
MMDDYY,YYMMDD,DDMMYY:begin
PadOut(Part1,2);
PadOut(Part2,2);
PadOut(Part3,2);
DStr := Part1+Sep1+Part2+Sep2+Part3;
end;
MMDDYYYY,DDMMYYYY:begin
PadOut(Part1,2);
PadOut(Part2,2);
PadOut(Part3,4);
DStr := Part1+Sep1+Part2+Sep2+Part3;
end;
YYYYMMDD:begin
PadOut(Part1,4);
PadOut(Part2,2);
PadOut(Part3,2);
DStr := Part1+Sep1+Part2+Sep2+Part3;
end;
MMYY:begin
PadOut(Part1,2);
PadOut(Part2,2);
DStr := Part1+Sep1+Part2;
end;
MMYYYY:begin
PadOut(Part1,2);
PadOut(Part2,4);
DStr := Part1+Sep1+Part2;
end;
end; {case}
PadDateStr := DStr;
end; {PadDateStr}
function GregtoJul(M,D,Y:longint):longint;
{}
var Factor: integer;
begin
if M < 3 then
Factor := -1
else
Factor := 0;
GregtoJul := (1461*(Factor+4800+Y) div 4)
+ ((M-2-(Factor*12))*367) div 12
- (3*((Y+4900+Factor) div 100) div 4)
+ D
- 32075;
end; {GregtoJul}
procedure JultoGreg(Jul:longint; var M,D,Y: longint);
{}
var U,V,W,X: longint;
begin
if Jul = 0 then {1.10a}
begin
M := 0;
D := 0;
Y := 0;
end
else
begin
inc(Jul,68569);
W := (Jul*4) div 146097;
dec(Jul,((146097*W)+3) div 4);
X := 4000*succ(Jul) div 1461001;
dec(Jul,((1461*X) div 4) - 31);
V := 80*Jul div 2447;
U := V div 11;
D := Jul - (2447*V div 80);
M := V + 2 - (U*12);
Y := X + U + (W-49)*100;
end;
end; {JultoGreg}
function Day(DStr:string;Format:tDate): word;
{}
var
DayStr: string;
begin
DStr := PadDateStr(DStr,Format);
case Format of
MMDDYY,
MMDDYYYY: DayStr := NthNumber(DStr,3)+NthNumber(DStr,4);
DDMMYY,
DDMMYYYY: DayStr := NthNumber(DStr,1)+NthNumber(DStr,2);
YYMMDD: DayStr := NthNumber(DStr,5)+NthNumber(DStr,6);
YYYYMMDD: DayStr := NthNumber(D